package com.lmqb.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmqb.bean.JCollection;
import com.lmqb.bean.LmAccount;
import com.lmqb.bean.LmAccountLog;
import com.lmqb.bean.LmCompanyAccount;
import com.lmqb.bean.LmCompanyAccountExtra;
import com.lmqb.bean.LmLoan;
import com.lmqb.bean.LmRepay;
import com.lmqb.bean.LmUser;
import com.lmqb.bean.LmUserBank;
import com.lmqb.bean.LmUserInfo;
import com.lmqb.bean.LmWithdraw;
import com.lmqb.dao.JCollectionMapper;
import com.lmqb.dao.LmAccountLogMapper;
import com.lmqb.dao.LmAccountMapper;
import com.lmqb.dao.LmCompanyAccountExtraMapper;
import com.lmqb.dao.LmCompanyAccountMapper;
import com.lmqb.dao.LmLoanMapper;
import com.lmqb.dao.LmRepayMapper;
import com.lmqb.dao.LmUserBankMapper;
import com.lmqb.dao.LmUserInfoMapper;
import com.lmqb.dao.LmUserMapper;
import com.lmqb.dao.LmWithdrawMapper;
import com.lmqb.entry.AgentAccount;
import com.lmqb.entry.Reward;
import com.lmqb.entry.Withdraw;
import com.lmqb.page.AccountLogPager;
import com.lmqb.page.AccountPager;
import com.lmqb.page.AgentAccountPager;
import com.lmqb.page.CollectionPager;
import com.lmqb.page.CompanyAccountPager;
import com.lmqb.page.CompanyExtraPager;
import com.lmqb.page.PageTool;
import com.lmqb.page.RewardPager;
import com.lmqb.page.UserAccountPager;
import com.lmqb.page.WithdrawPager;
import com.lmqb.service.AccountService;
import com.lmqb.util.StrUtil;

@Service("accountService")
public class AccountServiceImpl implements AccountService{

	@Autowired
	private LmUserMapper lmUserDao;
	
	@Autowired
	private LmAccountMapper accountDao;
	
	@Autowired
	private LmWithdrawMapper widthdrawDao;
	
	@Autowired
	private LmAccountLogMapper accountLogDao;
	
	@Autowired
	private LmCompanyAccountMapper companyAccountLogDao;
	
	@Autowired
	private LmCompanyAccountExtraMapper companyExtraDao;
	
	@Autowired
	private LmLoanMapper loanDao;
	
	@Autowired
	private JCollectionMapper collectionDao;
	
	@Autowired
	private LmRepayMapper repayDao;
	
	@Autowired
	private LmUserInfoMapper userInfoDao;
	
	@Autowired
	private LmUserBankMapper userBankDao;
	
	
	/**
	 * @show 现金账户
	 * 
	 * @param  AccountPager
	 * 
	 * @return AccountPager
	 * */
	public AccountPager selectAccountPager(AccountPager pagerModel) {
		List<LmAccount> list=accountDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = accountDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	/**
	 * @show 提现明细列表
	 * 
	 * @param  WithdrawPager
	 * 
	 * @return WithdrawPager
	 * */
	public WithdrawPager selectWithdrawPager(WithdrawPager pagerModel) {
		List<LmWithdraw> list=widthdrawDao.selectAll(pagerModel);
		List<LmUser> users=lmUserDao.selectAllUser();
		List<LmUserInfo> userInfos=userInfoDao.selectAll();
		List<LmUserBank> banks=userBankDao.selectAllUser();
		List<LmAccount> accounts=accountDao.selectAllUser();
		List<Withdraw> withdraws=new ArrayList<Withdraw>();
		for(int i=0;i<list.size();i++){
			Withdraw withdraw=new Withdraw();
			for(int j=0;j<accounts.size();j++){
				if(accounts.get(j).getUserId().equals(list.get(i).getUserId())){
					withdraw.setBalance(accounts.get(j).getAccountBalance());
					withdraw.setSumMoney(accounts.get(j).getTotalWithdraw());
				}
			}
			for(int k=0;k<banks.size();k++){
				if(banks.get(k).getBankId().equals(list.get(i).getBankId())){
					withdraw.setBankNum(banks.get(k).getBankName()+banks.get(k).getBankAccount());
				}
			}
			for(int m=0;m<users.size();m++){
				if(list.get(i).getUserId().equals(users.get(m).getUserId())){
					withdraw.setMobile(users.get(m).getLoginMobile());
					System.out.println(users.get(m).getNickName());
				withdraw.setUserName(users.get(m).getNickName());
					break;
				}
			}
			for(int n=0;n<userInfos.size();n++){
				if(userInfos.get(n).getUserId().equals(list.get(i).getUserId())){
					withdraw.setRealName(userInfos.get(n).getUserName());//用户名
				}
			}
			withdraw.setMoney(list.get(i).getWithdrawMoney());
			withdraw.setStatus(list.get(i).getPayStatus());
			withdraw.setUserId(list.get(i).getUserId());
			withdraw.setWithdrawTime(StrUtil.intToTime(list.get(i).getCreateTime()));
			withdraws.add(withdraw);
		}
		pagerModel.setList(withdraws);
		int count = widthdrawDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show 现金日志明细
	 * 
	 * @param  AccountLogPager
	 * 
	 * @return AccountLogPager
	 * */
	public AccountLogPager selectAccountLogPager(AccountLogPager pagerModel) {
		List<LmAccountLog> list=accountLogDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = accountLogDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show 公司所有出账明细
	 * 
	 * @param  CompanyAccountPager
	 * 
	 * @return CompanyAccountPager
	 * */
	public CompanyAccountPager selectCompanyAccountPager(
			CompanyAccountPager pagerModel) {
		List<LmCompanyAccount> list=companyAccountLogDao.selectAll(pagerModel);
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			list.get(i).setNotifyTimeD(StrUtil.intToTime(list.get(i).getNotifyTime()));
			list.get(i).setSubmitTimeD(StrUtil.intToTime(list.get(i).getSubmitTime()));
		}
		pagerModel.setList(list);
		int count = companyAccountLogDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	/**
	 * @show 公司所有进账明细
	 * 
	 * @param  CompanyExtraPager
	 * 
	 * @return CompanyExtraPager
	 * */
	public CompanyExtraPager selectCompanyExtraPager(
			CompanyExtraPager pagerModel) {
		List<LmCompanyAccountExtra> list=companyExtraDao.selectAll(pagerModel);
		pagerModel.setList(list);
		int count = companyExtraDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	 /**
	 * @show 代理商奖励惩罚
	 * 
	 * @param AgentAccountPager
	 * 
	 * @return AgentAccountPager
	 * */
	public AgentAccountPager selectAgentAccount(AgentAccountPager pagerModel) {
		List<LmAccountLog> list=accountLogDao.selectAllAgent(pagerModel);
		List<LmRepay> repays=repayDao.selectAllOrder();
		List<AgentAccount> agents=new ArrayList<AgentAccount>();
		for(int i=0;i<list.size();i++){
			AgentAccount agent=new AgentAccount();
			agent.setCuishou(null);
			agent.setCurrentStage(list.get(i).getCurrentStage());
			agent.setLoanAmount(list.get(i).getLoanAmount());
			agent.setMemId(list.get(i).getMemberId());
			agent.setMemName(list.get(i).getMemberName());
			
			agent.setTotalStage(list.get(i).getTotalStage());
			agent.setTradeMoney(list.get(i).getTradeMoney());
			agent.setUserId(list.get(i).getUserId());
			agent.setUserName(list.get(i).getUserName());
			if(repays!=null){
				for(int m=0;m<repays.size();m++){
					if(list.get(i).getRelateNo().equals(repays.get(m).getRepayNo())){
						agent.setRepayAmount(list.get(i).getLoanAmount().add(repays.get(m).getLoanInterest()).add(repays.get(m).getLoanLatefee()));
						agent.setOverDays(repays.get(m).getOverdueDays());
					}
				}
			}
			
			agents.add(agent);
		}
		pagerModel.setList(agents);
		int count = accountLogDao.selectCountAgent(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	
	 /**
	 * @show //催收统计
	 * 
	 * @param CollectionPager
	 * 
	 * @return CollectionPager
	 * */
	public CollectionPager selectCollectionPager(CollectionPager pagerModel) {
		List<JCollection> list=collectionDao.selectAll(pagerModel);
		for(int i=0;i<list.size();i++){
			list.get(i).setLoanTimeD(StrUtil.intToTime(list.get(i).getLoanTime()));
			list.get(i).setPlanRepayTimeD(StrUtil.intToTime(list.get(i).getPlanRepayTime()));
		}
		pagerModel.setList(list);
		int count = collectionDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	 /**
	 * @show 奖励明细
	 * 
	 * @param RewardPager
	 * 
	 * @return RewardPager
	 * */
	public RewardPager selectRewardPager(RewardPager pagerModel) {
		List<LmAccountLog> list=accountLogDao.selectAllReward(pagerModel);
		List<LmUser> users=lmUserDao.selectAllUser();
		List<LmUserInfo> userInfos=userInfoDao.selectAll();
		List<LmLoan> loans=loanDao.selectAllUser();
		List<LmRepay> repays=repayDao.selectAllOrder();
		List<Reward> rewards=new ArrayList<Reward>();
		for(int i=0;i<list.size();i++){
			Reward reward=new Reward();
			reward.setDownMobile(list.get(i).getMemberMobile());
			reward.setDownName(list.get(i).getMemberName());
			reward.setLoanMoney(list.get(i).getLoanAmount());
			for(int j=0;j<users.size();j++){
				
				if(users.get(j).getUserId()==list.get(i).getUserId()){
					//查找手机号
					reward.setMobile(users.get(j).getLoginMobile());
					reward.setUserName(users.get(j).getNickName());
					//查找推荐人
					for(int k=0;k<userInfos.size();k++){
						if(userInfos.get(k).getUserId()==users.get(j).getRecommendId()){
							reward.setUpName(userInfos.get(k).getUserName());
						}
					}
				}
			}
			//查找姓名
			for(int l=0;l<userInfos.size();l++){
				if(userInfos.get(l).getUserId()==list.get(i).getUserId()){
					reward.setRealName(userInfos.get(l).getUserName());
				}
			}
			for(int m=0;m<loans.size();m++){
				if(list.get(i).getRelateNo().equals(loans.get(m).getLoanNo())){
					reward.setLoanTime(StrUtil.intToTime(loans.get(m).getLoanTime()));
				}
			}
			for(int n=0;n<repays.size();n++){
				if(list.get(i).getRelateNo().equals(repays.get(n).getRepayNo())){
					reward.setLoanTime(StrUtil.intToTime(repays.get(n).getRepayYesTime()));
				}
			}
			reward.setRewardMoney(list.get(i).getTradeMoney());
			reward.setRewardTime(StrUtil.intToTime(list.get(i).getCreateTime()));
			reward.setUserId(list.get(i).getUserId());
			
			rewards.add(reward);
		}
		pagerModel.setList(rewards);
		int count = accountLogDao.selectCountReward(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	
	 /**
	 * @show 推广记录
	 * 
	 * @param RewardPager
	 * 
	 * @return RewardPager
	 * */
	public UserAccountPager selectTuiGuangPage(UserAccountPager pagerModel) {
		List<LmAccount> list=accountDao.selectTuiGuang(pagerModel);
//		List<User>
//		pagerModel.setList(list);
//		int count = companyExtraDao.selectCount(pagerModel);
//		pagerModel.setTotalRow(count);
//		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
//		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	@Override
	public int updateRepayStatusByRepayNo(Byte repayStatus,String repayNo) {
		
		return collectionDao.updateRepayStatusByRepayNo(repayStatus, repayNo);
	}

	//根据还款订单号查询信息
	public JCollection selectCuishou_repayno(JCollection jCollection) {
		return collectionDao.selectCuishou_repayno(jCollection);
	}

	//根据订单号修改
	public int updqteCuishou_repayno(JCollection jCollection){
		return collectionDao.updqteCuishou_repayno(jCollection);
	}
}
